package com.vay.de.dang.online.ui.apply;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.vay.de.dang.online.databinding.ActivityTakeImageBinding;
import com.vay.de.dang.online.util.PermissionUtil;
import com.vay.de.dang.online.R;
import com.vay.de.dang.online.util.VnUtil;
import com.vay.de.dang.online.util.CameraHelper;
import com.vay.de.dang.online.util.VnHandler;
import com.vay.de.dang.online.ui.WithBack1Activity;

// 拍照
public class TakeImageActivity extends WithBack1Activity {

    private ActivityTakeImageBinding binding;
    private int fromType = -1;
    private Bitmap takeBitmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        binding = ActivityTakeImageBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        // 0，证件照， 1.手持身份证, 2. 凭证
        if (getIntent() != null) {
            fromType = getIntent().getIntExtra("fromType", -1);
        }

        loadView();
    }

    private void loadView() {
        binding.bntCancel.setOnClickListener(view -> {
            finish();
        });

        binding.bntTake.setOnClickListener(view -> {
            if (takeBitmap != null) {
                binding.bntOk.setVisibility(View.INVISIBLE);
                binding.bntTake.setText("");
                VnUtil.recyBitmap(takeBitmap);
                takeBitmap = null;
                CameraHelper.startPreview();
            } else {
                CameraHelper.takePicture(image -> {
                    takeBitmap = image;
                    binding.bntOk.setVisibility(View.VISIBLE);
                    binding.bntTake.setText(R.string.label_take_image_again);
                });
            }
        });

        binding.bntOk.setOnClickListener(view -> {
            if (takeBitmap != null) {

                String path;
                // 需要裁减
                if (fromType == 0) {
                    Rect rect = binding.clipView.getClipRect(takeBitmap);
                    if (rect.width() <= 0 || rect.height() <= 0) {
                        showHint(getString(R.string.error_invalid_image));
                        return;
                    }
                    Bitmap dest = VnUtil.clipBitmap(takeBitmap, rect);
                    path = VnUtil.saveBitmapFile(dest);
                    // 埋点，用户证件正面照  拍照保存
                } else {
                    // 埋点，用户证件手持照保存
                    if (fromType == 1) {
                        path = VnUtil.saveBitmapFile(takeBitmap);
                    } else {
                        path = VnUtil.saveBitmapFile(takeBitmap, 90);
                    }
                }

                Intent data = new Intent();
                data.putExtra("_out_path", path);
                setResult(RESULT_OK, data);
                finish();
            }
        });

        // 切换摄像头
        binding.bntSwitch.setOnClickListener(v -> {

        });

        // 证件
        if (fromType == 0) {
            binding.clipView.setVisibility(View.VISIBLE);
            binding.bntSwitch.setVisibility(View.INVISIBLE);
            binding.preview.setCameraId(0);// 后置
        }
        // 凭证
        else if (fromType == 2) {
            binding.clipView.setVisibility(View.INVISIBLE);
            binding.bntSwitch.setVisibility(View.INVISIBLE);
            binding.preview.setCameraId(0);// 后置
        }
        // 手持
        else {
            binding.clipView.setVisibility(View.INVISIBLE);
            binding.bntSwitch.setVisibility(View.INVISIBLE);
            binding.preview.setCameraId(1); // 前置
        }

        if (!PermissionUtil.checkPermission(mActivity, PermissionUtil.getTakeImage())) {
            PermissionUtil.requestPermission(mActivity, PermissionUtil.getTakeImage(), (allGranted, grantedList, deniedList) -> {
                if (allGranted) {
                    VnHandler.runUIDelayed(() -> binding.preview.openCamera(), 500);
                } else {
                    showHint(R.string.error_camera_permission);
                    finish();
                }
            });
        } else {
            CameraHelper.startPreview();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        CameraHelper.stopPreview();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        VnUtil.recyBitmap(takeBitmap);
        //takeBitmap = null;
    }
}
